package com.wtwd.device.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wtwd.device.dao.AppDeviceSleepRecordMapper;
import com.wtwd.device.model.dto.DeviceSleepDto;
import com.wtwd.device.model.entity.DeviceSleepRecord;
import com.wtwd.device.model.entity.DeviceSleepRecordInfo;
import com.wtwd.device.model.vo.DeviceSleepRecordVo;
import com.wtwd.device.service.AppDeviceSleepRecordInfoService;
import com.wtwd.device.service.AppDeviceSleepRecordService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mjy
 * @since 2024-05-31
 */
@Service
public class DeviceSleepRecordServiceImpl extends ServiceImpl<AppDeviceSleepRecordMapper, DeviceSleepRecord> implements AppDeviceSleepRecordService {

    @Resource
    private AppDeviceSleepRecordMapper appDeviceSleepRecordMapper;

    @Resource
    private AppDeviceSleepRecordInfoService deviceSleepRecordInfoService;

    @Override
    public boolean insertOrUpdate(DeviceSleepRecord deviceSleepRecord) {
        if (deviceSleepRecord == null) {
            return false;
        }
        return appDeviceSleepRecordMapper.insertOrUpdate(deviceSleepRecord);
    }

    @Override
    public DeviceSleepRecordVo getDayDeviceSleepByTime(Long deviceId, String time) {
        return appDeviceSleepRecordMapper.getDayDeviceSleepByTime(deviceId, time);
    }

    @Override
    public List<DeviceSleepRecordVo> getDaysDeviceSleep(Long deviceId, String startTime, String endTime) {
        return appDeviceSleepRecordMapper.getDaysDeviceSleep(deviceId, startTime, endTime);
    }

    @Override
    public DeviceSleepRecord getLastDeviceSleepByDeviceId(Long deviceId) {
        return appDeviceSleepRecordMapper.getLastDeviceSleepByDeviceId(deviceId);
    }

    @Override
    public boolean synchronizationSleep(List<DeviceSleepDto> deviceSleepDtos) {
        for (DeviceSleepDto deviceSleepDto : deviceSleepDtos) {
            DeviceSleepRecord deviceSleepRecord = new DeviceSleepRecord();
            BeanUtils.copyProperties(deviceSleepDto,deviceSleepRecord);
            DeviceSleepRecordVo deviceSleepByTime = getDayDeviceSleepByTime(deviceSleepDto.getDeviceId(), deviceSleepDto.getTime());
            if (deviceSleepByTime!=null){//更新
                deviceSleepRecord.setId(deviceSleepByTime.getId());
                updateById(deviceSleepRecord);
            }else {//插入
                save(deviceSleepRecord);
            }
            List<DeviceSleepRecordInfo> deviceSleepRecordInfos = deviceSleepDto.getDeviceSleepRecordInfos();
            if (deviceSleepRecordInfos!=null&&deviceSleepRecordInfos.size()>0){
                deviceSleepRecordInfos.stream().forEach(e->{
                    e.setDeviceSleepRecordId(deviceSleepRecord.getId());
                });
                deviceSleepRecordInfoService.saveBatch(deviceSleepRecordInfos);
            }
        }

        return true;
    }

    @Override
    public boolean bathInsertOrUpdate(List<DeviceSleepRecord> deviceSleepRecords) {
        return appDeviceSleepRecordMapper.bathInsertOrUpdate(deviceSleepRecords) > 0 ? true : false;
    }
}
